package com.ac.service;

import com.ac.dao.BuildingDao;
import com.ac.entity.Building;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class BuildingService {

    @Autowired
    private BuildingDao buildingDao;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    public List<Building> getAll(){
        List<Building> buildings = null;
        if(redisTemplate.hasKey("buildings")){
            //直接读redis
            buildings = (List<Building>) redisTemplate.opsForValue().get("buildings");
        }else{
            //redis没有则读取数据库
            buildings = buildingDao.getAll();
            redisTemplate.opsForValue().set("buildings",buildings,1, TimeUnit.HOURS);
        }
        return buildings;
    }
}
